import { uEditDevBaseData } from '@/api/setting'
import { observable, computed, action } from 'mobx'
import { createContext } from 'react'
import ChildrenPageUtils from '@/store/extends'
import { uSelectDevStatusAndSimpleData } from '@/api/setting'
import Taro from '@tarojs/taro'

class Baseinfoedit extends ChildrenPageUtils {
  @observable pageQuery: { devId: string } = { devId: '' }
  @observable pageData: Setting.DevBaseInfo | null = null
  @observable devName: Setting.BaseInfoData | undefined = undefined
  @observable address: Setting.BaseInfoData | undefined = undefined
  @observable provinces: string[] | null = null

  @action.bound
  changeProvinces(val: string[]) {
    this.provinces = val
  }

  @action.bound
  async updatePageDataSource(
    cb?: (devName: string, address: string, provinces: string[]) => void
  ) {
    let res = await uSelectDevStatusAndSimpleData({
      devId: this.pageQuery.devId
    })

    if (res && res.result === 'success') {
      this.pageData = res.data

      const devName = this.pageData?.devEditDataPackList.filter(
        item => item.devBaseDataName === '设备名称'
      )[0]
      console.log(devName)

      const address = this.pageData?.devEditDataPackList.filter(
        item => item.devBaseDataName === '设备地址'
      )[0]

      this.devName = devName
      this.address = address

      let provinces = this.pageData?.devEditDataPackList
        .filter(item => ['省', '市', '区'].includes(item.devBaseDataName))
        .map(item =>
          item.devBaseDataValue !== 'N/A' && item.devBaseDataValue
            ? item.devBaseDataValue
            : ''
        )

      if (provinces.length) {
        this.provinces = provinces
      } else {
        this.provinces = null
      }

      cb &&
        cb(
          devName?.devBaseDataValue || '',
          address?.devBaseDataValue || '',
          provinces || []
        )
    } else {
      this.pageData = null
      this.devName = undefined
      this.address = undefined
      this.provinces = null

      cb && cb('', '', [])
    }
  }

  @action.bound
  async editDevBaseInfo({
    devName,
    address,
    provinces
  }: {
    devName?: string
    address?: string
    provinces?: string[]
  }) {
    let devBaseDataList: Setting.BaseInfoData[] = []
    this.pageData?.devEditDataPackList.forEach(item => {
      let devBaseData = {
        devBaseDataId: item.devBaseDataId,
        devBaseDataCode: item.devBaseDataCode,
        devBaseDataValue: '',
        devBaseDataName: item.devBaseDataName
      }

      switch (item.devBaseDataName) {
        case '设备名称':
          devBaseData.devBaseDataValue = devName || ''
          break
        case '设备地址':
          devBaseData.devBaseDataValue = address || ''
          break
        case '省':
          devBaseData.devBaseDataValue = provinces?.[0] || ''
          break
        case '市':
          devBaseData.devBaseDataValue = provinces?.[1] || ''
          break
        case '区':
          devBaseData.devBaseDataValue = provinces?.[2] || ''
          break
      }
      devBaseDataList.push(devBaseData)
    })

    let res = await uEditDevBaseData({
      devId: this.pageQuery.devId,
      // devBaseDataList: devBaseDataList
      devName: devBaseDataList[0].devBaseDataValue,
      devAddress: devBaseDataList[1].devBaseDataValue,
      province: devBaseDataList[2].devBaseDataValue,
      city: devBaseDataList[3].devBaseDataValue,
      district: devBaseDataList[4].devBaseDataValue
    })
    if (res) {
      if (res.result === 'success') {
        Taro.showToast({
          icon: 'success',
          title: '修改成功'
        })
      } else if (res.result === 'error' && res.msg) {
        Taro.showToast({
          icon: 'error',
          title: res.msg
        })
      }
    }
  }
}

export const baseInfoEditStore = createContext(new Baseinfoedit())
